package com.wyg.epdata.common.constant;

import com.alibaba.fastjson.annotation.JSONField;
import com.wyg.epcommonbase.common.util.file.StreamUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.stream.Stream;

/**
 * @Author: WangYongGan
 * @Date: 2021/10/30 11:40
 * @Description:
 */
@PropertySource({"classpath:config/values.properties"})
@Configuration
@Slf4j
public class BaseConstant implements InitializingBean {

    @Value("${imgDirPath}")
    String imgDirPath_;
    @Value("${info.url}")
    String serverUrl_;
    @Value("${aliyun.oss.file.endPoint}")
    private String endPoint_;
    @Value("${aliyun.oss.file.keyId}")
    private String keyId_;
    @Value("${aliyun.oss.file.keySecret}")
    private String keySecret_;
    @Value("${waitDownloadFile}")
    private long waitDownloadFile_;
    @Value("${aliyun.oss.file.bucketName}")
    @JSONField
    private String bucketName_;
    public static String END_POINT;
    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;
    //文件下载最大等待时间
    public static long waitDownloadFile;
    public static String BUCKET_NAME;
    public static String imgDirPath;
    public static String os = "windows";
    public static Random random = new Random();
    public static String urlEnontion = "http://10.16.16.234:8083/predictions/Emontion_model";
    public static String urlNer = "http://10.16.16.234:8083/predictions/Ner_model";
    public static String urlGetWords = "http://114.55.253.244:8002/getWords";
    public static String urlEntityIsValid = "http://localhost:5000";
    public static String webUrl = "http://localhost:8001";
    public static String fileLoadPath = "D:\\data\\java";
    public static String cmdWk = "wk ";
    public static String htmlBefore;
    public static String htmlAfter;
    public static String pdfDir;
    public static String htmlDir;
    //server url
    public static String serverUrl;

    @Override
    public void afterPropertiesSet() {
        if (System.getProperty("os.name").contains("inux"))
            linux();
        reloadData();
        serverUrl = serverUrl_;
        imgDirPath = imgDirPath_;
        END_POINT = endPoint_;
        ACCESS_KEY_ID = keyId_;
        ACCESS_KEY_SECRET = keySecret_;
        BUCKET_NAME = bucketName_;
        waitDownloadFile = waitDownloadFile_;
        pdfDir = BaseConstant.fileLoadPath + File.separator + "pdf" + File.separator;
        htmlDir = BaseConstant.fileLoadPath + File.separator + "html" + File.separator;

        Stream.of(pdfDir, htmlDir).forEach(o -> {
            File f = new File(o);
            final File[] files = f.listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    log.error("删除: " + file.getAbsolutePath());
                    if (file.getName().endsWith("html") || file.getName().endsWith("pdf")) {
                        file.delete();
                    }
                }
            }
        });

    }

    @SneakyThrows
    public void reloadData() {
        BufferedReader htmlReader = new BufferedReader(new InputStreamReader(getResources("config/index.html"), "UTF-8"));
        String html = StreamUtil.readerToString(htmlReader);
        htmlBefore = html.split("<!--more-->")[0];
        htmlAfter = html.split("<!--more-->")[2];
        log.info("html加载完毕");
    }

    public InputStream getResources(String path) {
        return getClass().getClassLoader().getResourceAsStream(path);
    }

    public static void main(String args[]) {
        System.out.println("java_home:" + System.getProperty("java.home"));//D:\java\jdk8u302-b08\jre
        System.out.println("user_name:" + System.getProperty("user.name"));//22453
        System.out.println("user_home:" + System.getProperty("user.home"));//C:\Users\22453
        System.out.println("user_dir:" + System.getProperty("user.dir"));//D:\project\boot-test1
        System.out.println(ClassLoader.getSystemResource(""));//file:/D:/project/boot-test1/target/classes/
        System.out.println(BaseConstant.class.getClassLoader().getResource(""));//同上
        File directory = new File("xxx");//设定为当前文件夹
        try {
            System.out.println(directory.getCanonicalPath());//获取标准的路径
            System.out.println(directory.getAbsolutePath());//获取绝对路径
        } catch (Exception e) {
        }
    }

    private void linux() {
        log.warn("linux系统");
        os = "linux";
        fileLoadPath = "/root/data";
        cmdWk = "sh /root/script/wk.sh ";
        serverUrl = "http://114.55.253.244:8001/";
        webUrl = serverUrl;
    }
}
